<?php

namespace app\api\controller;

use app\common\BaseHelper;

use app\admin\model\Userfarmremind as UserfarmremindMdl;
use app\admin\model\Image;

use think\Db;
use think\Exception;
use think\Request;

class Userfarmremind extends BaseController
{

    /**
     * 列表
     */
    public function getList()
    {
        $param = self::getHttpParam();

        $start = 0;
        $length = 20;

        if (empty($param->userId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'userId 不能为空');
        }
        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $userId = $param->userId;
        $where = [
            'userId' => $userId
        ];

        $model = new UserfarmremindMdl();
        $fields = '*';
        $order = 'createDate DESC';
        $result = $model->where($where)->field($fields)->limit($start, $length)->order($order)->select();
        $total = $model->where($where)->count();
        if (!$result) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '没有相关数据');
        }

        foreach ($result as $item) {
            switch ($item['repeat']) {
                case 0:
                    $item['perRepeat'] = '提醒一次';
                    break;
                case 1:
                    $item['perRepeat'] = '每'.$item['per'].'天';
                    break;
                case 2:
                    $item['perRepeat'] = '每'.$item['per'].'周';
                    break;
                case 3:
                    $item['perRepeat'] = '每'.$item['per'].'月';
                    break;
                case 4:
                    $item['perRepeat'] = '每'.$item['per'].'年';
                    break;
                default: break;
            }
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result, $total);
    }

    /**
     * 详情
     */
    public function getDetail()
    {
        $param = self::getHttpParam();

        if (empty($param->id)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'id 不能为空');
        }

        $where = [
            'id' => $param->id
        ];
        $model = new UserfarmremindMdl();
        $field = '*';
        $item = $model->where($where)->field($field)->find();
        if (!$item) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到相关数据');
        }

        switch ($item['repeat']) {
            case 0:
                $item['perRepeat'] = '提醒一次';
                break;
            case 1:
                $item['perRepeat'] = '每'.$item['per'].'天';
                break;
            case 2:
                $item['perRepeat'] = '每'.$item['per'].'周';
                break;
            case 3:
                $item['perRepeat'] = '每'.$item['per'].'月';
                break;
            case 4:
                $item['perRepeat'] = '每'.$item['per'].'年';
                break;
            default: break;
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $item);
    }

    /**
     * 保存
     */
    public function save()
    {
        $param = self::getHttpParam();

        if (empty($param->userId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'userId 不能为空');
        }
        if (empty($param->title)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '提醒事项(title) 不能为空');
        }
        if (empty($param->time)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '提醒时间(time) 不能为空');
        }
        if (empty($param->repeat) && $param->repeat != 0) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '重复提醒(repeat) 不能为空');
        }
        if($param->repeat != 0){
            if (empty($param->per)) {
                return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '每(per) 不能为空');
            }
        }
        if (empty($param->mode)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '提醒方式(mode) 不能为空');
        }

        $userId = $param->userId;

        Db::startTrans();
        try {
            $data = (array)$param;
            $model = new UserfarmremindMdl();

            $data['time'] = strtotime($data['time']);
            if (!empty($param->id)) {
                $id = $param->id;
                $data['updateDate'] = time();
                $data['userId'] = $userId;

                $result = $model->allowField(true)->save($data, ['id' => $id]);
            } else {
                $data['id'] = BaseHelper::getUUID();
                $data['createDate'] = time();

                $result = $model->save($data);
            }

            if($result !== false){
                Db::commit();
            }else{
                Db::rollback();
                return show(config('status.ERROR_STATUS'), '保存失败', $model->getError());
            }
        } catch (Exception $e) {
            Db::rollback();
            return show(config('status.ERROR_STATUS'), '保存失败', $e->getMessage());
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $data);
    }

    /**
     * 删除
     */
    public function delete()
    {
        $param = self::getHttpParam();

        if (empty($param->id)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'id 不能为空');
        }

        $where = [
            'id' => $param->id
        ];
        $model = new UserfarmremindMdl();
        $result = $model->where($where)->delete();
        if ($result === false) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '删除失败');
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result);
    }

}